package com.learn.use.myreconsituteproject.login.widget;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.learn.use.myreconsituteproject.MainActivity;
import com.learn.use.myreconsituteproject.R;
import com.learn.use.myreconsituteproject.forgotpass.ForgetPassActivity;
import com.learn.use.myreconsituteproject.login.bean.LoginBean;
import com.learn.use.myreconsituteproject.login.presenter.LoginPresenter;
import com.learn.use.myreconsituteproject.login.view.LoginView;
import com.learn.use.myreconsituteproject.mvp.BaseActivity;
import com.learn.use.myreconsituteproject.register.RegisterActivity;
import com.learn.use.myreconsituteproject.util.SharedPreferencesUtil;
import com.learn.use.myreconsituteproject.util.ViewUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 聪明一只哈 on 2018/9/11.
 * Time: 2018/9/11  16:21
 */
public class LoginActivity extends BaseActivity<LoginView, LoginPresenter<LoginView>> implements LoginView {

    @BindView(R.id.login_image)
    ImageView loginImage;
    @BindView(R.id.id_count_num)
    EditText idCountNum;
    @BindView(R.id.ll_login_a)
    LinearLayout llLoginA;
    @BindView(R.id.line_user_login_a)
    View lineUserLoginA;
    @BindView(R.id.id_count_pass)
    EditText idCountPass;
    @BindView(R.id.ll_login_b)
    LinearLayout llLoginB;
    @BindView(R.id.line_user_login_b)
    View lineUserLoginB;
    @BindView(R.id.tv_login_register)
    TextView tvLoginRegister;
    @BindView(R.id.tv_login_forget)
    TextView tvLoginForget;
    @BindView(R.id.bt_login)
    Button btLogin;
    @BindView(R.id.pb)
    ProgressBar progressBar;
    private String num;
    private String pass;
    private String ase_simple;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtil.initSystemBar(this, R.color.system_bar_bg);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        ase_simple = SharedPreferencesUtil.getString(this, "ase_simple", "");

        String num=getIntent().getStringExtra("user_regist_and_fogt_cell_reset");
        String pass=getIntent().getStringExtra("user_regis_pwd");
        if (num!=null&&pass!=null&&!TextUtils.isEmpty(num)&&!TextUtils.isEmpty(pass)){
            idCountNum.setText(num);
            idCountPass.setText(pass);
        }
    }

    @Override
    protected LoginPresenter<LoginView> creatPresenter() {
        return new LoginPresenter<>(this);
    }

    @OnClick({R.id.tv_login_register, R.id.tv_login_forget, R.id.bt_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_login_register:
                ViewUtil.startActivity(LoginActivity.this, RegisterActivity.class);
                break;
            case R.id.tv_login_forget:
                ViewUtil.startActivity(LoginActivity.this, ForgetPassActivity.class);
                break;
            case R.id.bt_login:
                num = idCountNum.getText().toString().trim();
                pass = idCountPass.getText().toString().trim();
                if (TextUtils.isEmpty(num)||TextUtils.isEmpty(pass)){
                   new ViewUtil().showToast(this,"手机号或密码格式不正确");
                  }else{
                    mPresenter.getLoginData(num, pass);
                }
                break;
        }
    }

    @Override
    public void showProgress() {
        progressBar.setVisibility(View.VISIBLE);

    }

    @Override
    public void dismissProgress() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void getLoginData(LoginBean loginBean) {
        Log.i("登录结果", "getLoginData: "+loginBean.getReason());
        LoginBean.DataBean data = loginBean.getData();
        //判断集合
        if (data != null) {

            String M_id = data.getM_id();
            SharedPreferencesUtil.saveString(LoginActivity.this, "App_Only_Id", M_id);
            String ee = data.getError();
            //请求失败的json串
            if (ee != null && !ee.equals("")) {
                Toast.makeText(LoginActivity.this, ee, Toast.LENGTH_SHORT).show();
            } else {
                //请求成功的json串的解析
                String reason = loginBean.getReason();
                if ("1".equals(reason)) {
                    SharedPreferencesUtil.saveString(LoginActivity.this,"count_num",num);
                    SharedPreferencesUtil.saveString(LoginActivity.this,"count_pwd",pass);

                    Intent intent_login = new Intent(LoginActivity.this, MainActivity.class);
                    intent_login.putExtra("m_id", M_id);
                    startActivity(intent_login);
                    finish();

                }else if("2".equals(reason)){
                    SharedPreferencesUtil.DeleteData(LoginActivity.this);
                    Toast.makeText(LoginActivity.this, data.getError(), Toast.LENGTH_SHORT).show();
                    SharedPreferencesUtil.saveString(LoginActivity.this, "ase_simple", ase_simple);
                }

            }

        }


    }

    @Override
    public void getFiledData(String errorMessage) {
        Log.i("登录结果", "getLoginData: "+errorMessage);
    }


}
